<?PHP
	require 'includes/master.inc.php';
	$auth->admin();

	$app = new App($_GET['app_id']);
	if($app->id == "") redirect("index.php");

	if(isset($_POST['btncancel']))
		redirect("index.php");
	
	if(isset($_POST['btndelete']))
	{
		$app->delete();
		redirect("index.php");
	}
	
	if(isset($_POST['btnsubmit']))
	{
		$Error->blank($_POST['name'], "name");
		if($_POST['upload_type'] == "s3")
		{
			$Error->blank($_POST['s3key'], "S3 Key");
			$Error->blank($_POST['s3pkey'], "S3 Private Key");
			$Error->blank($_POST['s3bucket'], "S3 Bucket");
			$Error->blank($_POST['s3path'], "S3 Path");
		}
		
		if($Error->ok())
		{
			$app->name          = $_POST['name'];
			$app->description   = $_POST['description'];
			$app->link          = $_POST['link'];
			$app->use_md5       = $_POST['md5'] == 1 ? 1 : 0;
			$app->upload_type   = $_POST['upload_type'];
			$app->s3key         = $_POST['s3key'];
			$app->s3pkey        = $_POST['s3pkey'];
			$app->s3bucket      = $_POST['s3bucket'];
			$app->s3path        = $_POST['s3path'];
			$app->ap_key        = $_POST['ap_key'];
			$app->ap_pkey       = $_POST['ap_pkey'];
			$app->ap_email      = $_POST['ap_email'];
			$app->ap_bcc        = $_POST['ap_bcc'];
			$app->ap_subject    = $_POST['ap_subject'];
			$app->ap_body       = $_POST['ap_body'];
			$app->ap_license_ext = $_POST['ap_license_ext'];
			$app->sparkle_name  = $_POST['sparkle_name'];
			$app->update();
			redirect("editapp.php?app_id=" . $app->id);
		}
		else
		{
			$name          = htmlspecialchars($_POST['name']);
			$link          = htmlspecialchars($_POST['link']);
			$description   = htmlspecialchars($_POST['description']);
			$md5           = $_POST['md5'] == 1 ? "checked='checked'" : "";
			$upload_type   = htmlspecialchars($_POST['upload_type']);
			$s3key         = htmlspecialchars($_POST['s3key']);
			$s3pkey        = htmlspecialchars($_POST['s3pkey']);
			$s3bucket      = htmlspecialchars($_POST['s3bucket']);
			$s3path        = htmlspecialchars($_POST['s3path']);
			$ap_key        = htmlspecialchars($_POST['ap_key']);
			$ap_pkey       = htmlspecialchars($_POST['ap_pkey']);
			$ap_email      = htmlspecialchars($_POST['ap_email']);
			$ap_bcc        = htmlspecialchars($_POST['ap_bcc']);
			$ap_subject    = htmlspecialchars($_POST['ap_subject']);
			$ap_body       = htmlspecialchars($_POST['ap_body']);
			$ap_license_ext = htmlspecialchars($_POST['ap_license_ext']);
			$sparkle_name  = htmlspecialchars($_POST['sparkle_name']);
		}
	}
	else
	{
		// Blank our echo vars
		$name          = htmlspecialchars($app->name);
		$link          = htmlspecialchars($app->link);
		$description   = htmlspecialchars($app->description);
		$md5           = $app->use_md5 == 1 ? "checked='checked'" : "";
		$upload_type   = htmlspecialchars($app->upload_type);
		$s3key         = htmlspecialchars($app->s3key);
		$s3pkey        = htmlspecialchars($app->s3pkey);
		$s3bucket      = htmlspecialchars($app->s3bucket);
		$s3path        = htmlspecialchars($app->s3path);
		$ap_key        = htmlspecialchars($app->ap_key);
		$ap_pkey       = htmlspecialchars($app->ap_pkey);
		$ap_email      = htmlspecialchars($app->ap_email);
		$ap_bcc        = htmlspecialchars($app->ap_bcc);
		$ap_subject    = htmlspecialchars($app->ap_subject);
		$ap_body       = htmlspecialchars($app->ap_body);
		$ap_license_ext = htmlspecialchars($app->ap_license_ext);
		$sparkle_name  = htmlspecialchars($app->sparkle_name);
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<title>Editing <?PHP echo $name;?> | Appcaster</title>
	<?PHP include 'inc/meta.inc.php'; ?>
	<?PHP $Error->css(); ?>
</head>

<body id="home">
	<?PHP include 'inc/head.inc.php'; ?>
	<div id="nav">
		<ul>
			<li class='selected'><a href='<?PHP WEBROOT();?>index.php'>Applications</a></li>
			<li><a href='<?PHP WEBROOT();?>orders.php'>Orders</a></li>
			<li><a href='<?PHP WEBROOT();?>feedback.php'>Feedback (<?PHP echo Feedback::newCountTotal();?>)</a></li>
			<li><a href='<?PHP WEBROOT();?>stats.php'>Stats</a></li>
			<li><a href='<?PHP WEBROOT();?>options.php'>Options</a></li>
		</ul>
	</div>

	<div id="menutabs">
		<ul id="tabnav">
			<li><a href='<?PHP WEBROOT();?>index.php'>Application List</a></li>
			<li><a href='<?PHP WEBROOT();?>newapp.php'>New App</a></li>
			<li class='selected'><a href='<?PHP WEBROOT();?>editapp.php?app_id=<?PHP echo $app->id;?>'><?PHP echo $app->__name;?></a></li>
			<li><a href='<?PHP WEBROOT();?>versions.php?app_id=<?PHP echo $app->id;?>'>Versions</a></li>
		</ul>
	</div>

	<div id="contain">
		<?PHP echo $Error;?>
		<form action="" method="post">
			<fieldset>
				<legend>Application Info</legend>
				<p><label for="name">Name:</label> <input type="text" class="text" name="name" value="<?PHP echo $name; ?>" id="name"/></p>
				<p><label for="link">Link:</label> <input type="text" class="text" name="link" value="<?PHP echo $link;?>" id="link"/> A link to your app's website.</p>
				<p><label for="sparkle_name">Sparkle Name</label> <input type="text" name="sparkle_name" value="<?PHP echo $sparkle_name;?>" id="sparkle_name"/> Usually YourApp.app</p>
				<p><label for="description">Description:</label><br/><textarea name="description" id="description" class="medium"><?PHP echo $description; ?></textarea></p>
				<p><input type="checkbox" name="md5" value="1" <?PHP echo $md5; ?> id="md5"/> <label for="md5"><a href="http://sparkle.andymatuschak.org/wiki/Documentation/Security">Calculate MD5 hash for downloads</a>?</label></p>
			</fieldset>
			<fieldset>
				<legend>Upload Info (Where will your app's downloads be stored?)</legend>
				<p>
					<label for="">Upload Type:</label>
					<input type="radio" name="upload_type" value="local" id="upload_type_local"/> <label for="upload_type_local">Locally on this server</label> or in
					<input type="radio" name="upload_type" value="s3" checked="checked" id="upload_type_s3"/> <label for="upload_type_s3">Amazon S3</label>
					
					<p><label for="s3key">S3 Key:</label> <input type="text" name="s3key" value="<?PHP echo $s3key; ?>" class="text" id="s3key"/></p>
					<p><label for="s3pkey">S3 Private Key:</label> <input type="text" name="s3pkey" value="<?PHP echo $s3pkey;?>" class="text" id="s3pkey"/></p>
					<p><label for="s3bucket">S3 Bucket Name:</label> <input type="text" name="s3bucket" value="<?PHP echo $s3bucket;?>" class="text" id="s3bucket"/></p>
					<p><label for="s3path">S3 Path:</label> <input type="text" name="s3path" value="<?PHP echo $s3path;?>" class="text" id="s3path"/> Typically http://bucketname.s3.amazonaws.com/your-path/</p>
				</p>
			</fieldset>
			<fieldset>
				<legend>AquaticPrime Info</legend>
				<p><label for="ap_key">Public Key:</label> <input type="text" name="ap_key" value="<?PHP echo $ap_key;?>" class="text" id="ap_key"/></p>
				<p><label for="ap_pkey">Private Key:</label> <input type="text" name="ap_pkey" value="<?PHP echo $ap_pkey;?>" class="text" id="ap_pkey"/></p>
				<p><label for="ap_email">From Email:</label> <input type="text" name="ap_email" value="<?PHP echo $ap_email;?>" id="ap_email" class="text"/> Email address that your license files will be sent from.</p>
				<p><label for="ap_bcc">BCC Email:</label> <input type="text" name="ap_bcc" value="<?PHP echo $ap_bcc;?>" id="ap_bcc" class="text"/> Send a copy of the license email to this address. (optional)</p>
				<p><label for="ap_subject">Subject Line:</label> <input type="text" name="ap_subject" value="<?PHP echo $ap_subject;?>" id="ap_subject" class="text"/></p>
				<p><label for="ap_body">Message Body:</label><br/><textarea name="ap_body" id="ap_body" class="large"><?PHP echo $ap_body; ?></textarea></p>
				<p><label for="ap_license_ext">License Extension:</label> <input type="text" name="ap_license_ext" value="<?PHP echo $ap_license_ext;?>" id="ap_license_ext"/> Extension of the email license attachement.</p>
			</fieldset>
			<p>
				<input type="submit" name="btnsubmit" value="Save App" id="btnsubmit"/>
				<input type="submit" name="btncancel" value="Cancel" id="btncancel"/>
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				<input type="submit" name="btndelete" value="Delete App" id="btndelete"/>
			</p>
		</form>
	</div>
</body>
</html>
